package com.qidian.buy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qidian.buy.mapper.ItemSpecificationsMapper;
import com.qidian.buy.pojo.ItemSpecifications;
import com.qidian.buy.service.ItemSpecificationsService;
import com.qidian.buy.util.QdConstant;
import com.qidian.buy.util.pojo.QdResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author 戴着假发的程序员
 * @company 起点编程
 * @date 2023/9/8 11:04
 */
@Service
public class ItemSpecificationsServiceImpl implements ItemSpecificationsService {
    @Autowired
    private ItemSpecificationsMapper speMapper;
    @Override
    public QdResult queryByPage(ItemSpecifications spe, int pageNum, int limit) {
        IPage<ItemSpecifications> page = new Page<>(pageNum,limit);
        QueryWrapper<ItemSpecifications> wrapper = new QueryWrapper<>();
        wrapper.ne("status", QdConstant.DATA_DELETED);
        wrapper.like(spe != null && spe.getSpeName() != null && !spe.getSpeName().equals(""),"spe_name",spe.getSpeName());
        wrapper.eq(spe != null && spe.getStatus() != null && spe.getStatus() != 0,"status",spe.getStatus());
        return QdResult.ok(speMapper.selectPage(page,wrapper));
    }

    @Override
    public QdResult save(ItemSpecifications spe) {
        // 校验是否重复
        QueryWrapper<ItemSpecifications> wrapper = new QueryWrapper<>();
        wrapper.ne("status", QdConstant.DATA_DELETED);
        wrapper.eq(spe != null && spe.getSpeName() != null && !spe.getSpeName().equals(""),"spe_name",spe.getSpeName());
        if(speMapper.selectOne(wrapper) != null){
            return QdResult.duplicate("规格名称重复");
        }
        spe.setModifyTime(new Date());
        spe.setCreateTime(new Date());
        return QdResult.isOk(speMapper.insert(spe));
    }

    @Override
    public QdResult queryById(Long speId) {
        return QdResult.ok(speMapper.selectById(speId));
    }
}
